package com.vinoxm.rss.dao;

import com.vinoxm.rss.model.RssCopyright;
import com.vinoxm.rss.model.RssDetail;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface RssCopyrightDao extends RssDetailDao {

    @Insert("<script>" +
            "INSERT INTO rss_copyright " +
            "(pid, area, href, image) " +
            "VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.pid},#{item.area},#{item.href},#{item.image})" +
            "</foreach>" +
            " ON DUPLICATE KEY UPDATE area=VALUES(area),href=VALUES(href),image=VALUES(image)" +
            "</script>")
    void insertMany(@Param("list") List<? extends RssDetail> list);

    @Insert("<script>" +
            "INSERT INTO rss_copyright " +
            "(pid, area, href, image) " +
            "VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{pid},#{item.area},#{item.href},#{item.image})" +
            "</foreach>" +
            " ON DUPLICATE KEY UPDATE area=VALUES(area),href=VALUES(href),image=VALUES(image)" +
            "</script>")
    void insertManyWithPid(@Param("list") List<? extends RssDetail> list, @Param("pid") long pid);

    @Select("SELECT id,pid,area,href,image FROM rss_copyright WHERE pid=#{pid} ORDER BY id")
    List<RssCopyright> selectByPid(long pid);

    @Insert("INSERT INTO rss_copyright (pid, area, href, image) VALUES (#{pid},#{area},#{href}, #{image}) ON DUPLICATE KEY UPDATE area=VALUES(area),href=VALUES(href),image=VALUES(image)")

    int insertOne(RssCopyright rssLink);

    @Update("UPDATE rss_copyright SET area=#{area},href=#{href},image=#{image} WHERE id=#{id}")
    int updateOne(RssCopyright rssLink);

    @Delete("DELETE FROM rss_copyright WHERE id=#{id}")
    int deleteOne(long id);

    @Delete("DELETE FROM rss_copyright WHERE pid=#{pid}")
    int deleteManyByPid(long pid);

    @Delete("<script>" +
            "DELETE FROM rss_copyright WHERE pid IN (" +
            "<foreach collection='pids' item='item' separator=','>" +
            "#{item}" +
            "</foreach>" +
            ")" +
            "</script>")
    int deleteManyByPids(List<Long> pids);

    @Select("SELECT pid FROM rss_copyright WHERE id=#{id}")
    Long selectPidById(long pid);

    @Select("SELECT image FROM rss_copyright GROUP BY image")
    List<String> selectAllImage();
}
